/*
 * WizardFrame.java
 *
 * Created on April 7, 2008, 7:57 PM
 */

package protovitae.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author  krattp
 */
public class WizardFrame extends javax.swing.JFrame {
    private int mytab;
    private WelcomePanel pnlWelcome; //As there are only two tabs, declaring them up appear appears to be reasonable.
    private SimSetupPanel pnlSetup;
   
    /** Creates new form WizardFrame */
    public WizardFrame() {
        initComponents();
        initPosition();
        showSplashScreen();
        mytab = 0;
        pnlWelcome = new WelcomePanel();
        pnlSetup = new SimSetupPanel();
        pnlTabs.add("Welcome",pnlWelcome);
    }
    
    private void initPosition() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width / 2 - (this.getWidth() / 2), screenSize.height / 2 - (this.getHeight() / 2));
    }
    
    private void showSplashScreen() {
        // image based on stock photo by ~chaos-420:iconchaos-420:  at 
        // http://chaos-420.deviantart.com/art/DNA-12941824
        SplashWindow splash = new SplashWindow((javax.swing.JFrame)this, new File("splash.jpg"), 2000);
        splash.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grpNewLoad = new javax.swing.ButtonGroup();
        btnNext = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        pnlTabs = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ProtoVitae Welcome Wizard");

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnPrev.setText("Previous");
        btnPrev.setEnabled(false);
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });

        pnlTabs.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPrev)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                .addComponent(btnNext)
                .addContainerGap())
            .addComponent(pnlTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnlTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNext)
                    .addComponent(btnPrev))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        if(mytab == 0){
            if(pnlWelcome.getSelectedOption()==0){
                //User selected new simulation
                mytab++; //Set tab to 1
                pnlTabs.remove(pnlWelcome);
                btnPrev.setEnabled(true);
                pnlTabs.add("Simulation Properties",pnlSetup);
                pnlTabs.revalidate(); //Redraw and resize the panel
            }
            else{
                //User selected Load file
                JFileChooser fbox = new JFileChooser();
                
                fbox.setFileFilter(new FileXMLFilter());
                if(fbox.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
                    MainWindow mainwin = new MainWindow(fbox.getSelectedFile());
                    mainwin.setVisible(true);
                }
            }
        }
        else if(mytab == 1){
            int cycles = pnlSetup.getCycles();
            int healthmod = pnlSetup.getHealthMod();
            
            if(cycles > 0 && healthmod < 200 && healthmod > -200){
                MainWindow mainwin = new MainWindow();

                mainwin.getSimulation().setCycles(cycles);
                mainwin.getSimulation().setHealthmod(healthmod);
                mainwin.setVisible(true);
                this.dispose(); //Close the wizard
            }
        }
        else{
            System.out.println("Not enough tab handles."); //This will never happen
            System.exit(0); //Weird problem, bail out.
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        if(mytab > 0){
            mytab--;
            if(mytab==0){
                btnPrev.setEnabled(false);
                pnlTabs.remove(pnlSetup);
                pnlTabs.add("Welcome",pnlWelcome);
                pnlTabs.revalidate(); //Redraw and resize the panel
            }
        }
    }//GEN-LAST:event_btnPrevActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WizardFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.ButtonGroup grpNewLoad;
    private javax.swing.JPanel pnlTabs;
    // End of variables declaration//GEN-END:variables
    
}
